package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 课程展示
func ListKe(c *gin.Context) {
	var req request.ListKe
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	ke, err := config.UserClient.ListKe(c, &__.ListKeReq{})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "课程展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "课程展示成功",
		"data": ke,
	})
}

// 课程删除
func DeleteKe(c *gin.Context) {
	var req request.DeleteKe
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	_, err := config.UserClient.DeleteKe(c, &__.DeleteKeReq{KeId: int64(req.Id)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "删除课程失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "删除课程成功",
		"data": true,
	})
}

// 课程分页
func KeList(c *gin.Context) {
	var req request.KeList
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	ke, err := config.UserClient.KeList(c, &__.KeListReq{
		Page:     req.Page,
		PageSize: req.PageSize,
		Title:    req.Title,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "课程展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "课程展示成功",
		"data": ke,
	})
}

// 课程详情
func Detail(c *gin.Context) {
	var req request.Detail
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	detailKe, err := config.UserClient.DetailKe(c, &__.DetailKeReq{KeId: int64(req.Id)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "课程详情展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "课程详情展示成功",
		"data": detailKe,
	})
}

// 用户收藏表
func CollectUser(c *gin.Context) {
	var req request.CollectUser
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	detailKe, err := config.UserClient.CollectUser(c, &__.CollectUserReq{Id: int64(req.Id)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "展示成功",
		"data": detailKe,
	})
}
